<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
    <div class="demo-input-size">
        <el-input
                size="medium"
                placeholder="请输入职称名"
                suffix-icon="el-icon-date"
                style="display: inline-block;width: 200px;"
                v-model="searchInfo.pname"
        >
        </el-input>
        <el-button icon="el-icon-search" type="success"  @click="searchPost()">搜索职称</el-button>
        <el-button icon="el-icon-circle-plus-outline" type="warning"  @click="showAddDialog()">添加职称</el-button>
        <el-button icon="el-icon-sort" type="warning"   @click="exportPost()">导出</el-button>
        <a style="text-decoration: none" href="/ssm/posttemp.xlsx">下载模板</a>
        <form id="upform">
            <input name="haha" type="file">
        </form>
        <el-button icon="el-icon-sort" type="warning" @click="uploadPost()" >导入职称信息</el-button>

        <el-table
                :data="PostArr"
                border
                style="width: 100%">
            <el-table-column
                    prop="id"
                    label="编号"
                    width="80">
            </el-table-column>
            <el-table-column
                    prop="pname"
                    label="职称"
            >
            </el-table-column>
            <el-table-column
                    prop="state"
                    label="状态">
                <template slot-scope="scope">
                     <span v-if="scope.row.state== 0">
                         <div class="state-icon icon0" style="color: #409EFF;font-weight: bolder">启动</div>
                     </span>
                     <span v-if="scope.row.state== 1">
                         <div class="state-icon icon1" style="color: red;font-weight: bolder">禁用</div>
                     </span>
                     <span v-if="scope.row.state== 2">
                         <div class="state-icon icon2" style="color: blueviolet;font-weight: bolder">冻结</div>
                     </span>
                </template>
            </el-table-column>
            <el-table-column
                    prop="dept.dname"
                    label="部门"
            >
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
            >
                <template slot-scope="scope">
                    <el-button icon="el-icon-delete-solid" type="warning" circle @click="deletePost(scope.row.id)"></el-button>
                    <el-button icon="el-icon-s-tools" type="primary" circle @click="showPost(scope.row.id)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                @current-change="handleCurrentChange"
                layout="prev, pager, next"
                :total="totalCount">
        </el-pagination>
    </div>

    <el-dialog title="添加职位" :visible.sync="dialogFormVisible">
        <el-form :model="postInfo" :rules="rules" ref="postInfo">
            <el-form-item label="职称名称" prop="pname">
                <el-input v-model="postInfo.pname"></el-input>
            </el-form-item>
            <el-form-item label="职称状态" prop="state">
                <el-select v-model="postInfo.state" placeholder="请选择职称状态">
                    <el-option label="0:开启" value="0"></el-option>
                    <el-option label="1:关闭" value="1"></el-option>
                    <el-option label="2:冻结" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="该职称对应的部门" prop="did">
                <el-select v-model="postInfo.did" placeholder="请选择岗位">
                    <el-option v-for="(d,i) in deptArr" :label="d.dname" :value="d.id"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addPost()" >添 加</el-button>
        </div>
    </el-dialog>

    <el-dialog title="修改职位" :visible.sync="updatedialogFormVisible">
        <el-form :model="postInfo" :rules="rules" ref="postInfo">
            <el-form-item label="职称名称" prop="pname">
                <el-input v-model="postInfo.pname"></el-input>
            </el-form-item>
            <el-form-item label="职称状态" prop="state">
                <el-select v-model="postInfo.state" placeholder="请选择职称状态">
                    <el-option label="0:开启" value="0"></el-option>
                    <el-option label="1:关闭" value="1"></el-option>
                    <el-option label="2:冻结" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="该职称对应的部门" prop="did">
                <el-select v-model="postInfo.did" placeholder="请选择岗位">
                    <el-option v-for="(d,i) in deptArr" :label="d.dname" :value="d.id"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="updatedialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updatePost()" >修 改</el-button>
        </div>
    </el-dialog>
</div>
<script>
    var danghui = new Vue({
        el: "#app",
        data: {
            searchInfo: {},
            PostArr: [],
            totalCount: 0,
            postInfo:{},
            deptArr:[],
            dialogFormVisible:false,
            updatedialogFormVisible:false,
            rules: {
                pname: [
                    {required: true, message: '请输入职位名称', trigger: 'blur'},
                ],
                state: [
                    {required: true, message: '请输入此科室的状态', trigger: 'blur'},
                ],
                did: [
                    {required: true, message: '请选择该职位对应的部门', trigger: 'change'},
                ],
            }
        },
        methods: {

            uploadPost(){
                getData(1,10);
                //使用js写好的formdata类  读取表单中的信息
                var PostData = new FormData(upform);
                //通过Ajax将data发送给后台  $.post
                $.ajax({
                    url:"/ssm/PostSource/upload",
                    type:"POST",
                    data:PostData,
                    processData:false,
                    contentType:false,
                    success:function(backData){
                    }
                });
            },

            exportPost(){
                window.location.href = "/ssm/PostSource/download?name="+danghui.searchInfo.pname+"&pageNo=1&pageCount=100";
            },

            updatePost(){
                danghui.$refs["postInfo"].validate((valid) => {
                    if (valid) {
                        $.post("/ssm/post/update", danghui.postInfo, function (backData) {
                            if (backData.code == 1) {
                                danghui.updatedialogFormVisible = false;
                                danghui.$refs["postInfo"].resetFields();
                                danghui.$message.success("修改成功");
                                getData(1, 10);
                            } else {
                                danghui.$message.error("修改失败");
                            }
                        });
                    }
                });
            },

            showPost(id){
                $.get("/ssm/dept/list",function (backData) {
                    danghui.deptArr = backData.data;
                    $.get("/ssm/post/get/"+id,function (backData) {
                        danghui.postInfo = backData.data;
                        danghui.updatedialogFormVisible = true;
                    })
                })
            },

            addPost(){
                danghui.$refs["postInfo"].validate((valid) => {
                    if (valid) {
                        $.post("/ssm/post/add", danghui.postInfo, function (backData) {
                            if (backData.code == 1) {
                                danghui.dialogFormVisible = false;
                                danghui.$refs["postInfo"].resetFields();
                                danghui.$message.success("添加成功");
                                getData(1, 10);
                            } else {
                                danghui.$message.error("添加失败");
                            }
                        });
                    }
                });
            },
            showAddDialog(){
                $.get("/ssm/dept/list",function (backData) {
                    danghui.deptArr = backData.data;
                    danghui.dialogFormVisible = true;
                })
            },

            deletePost(id) {
                danghui.$confirm('此操作将永久删除该职称, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.get("/ssm/post/delete/" + id, function (backData) {
                        danghui.$message.success(backData.msg);
                        getData(1, 10)
                    });
                });
            },

            searchPost() {
                getData(1, 10);
            },
            handleCurrentChange(val) {
                getData(val, 10)
            }
        }
    });

    function getData(a, b) {
        danghui.searchInfo.pageNo = a;
        danghui.searchInfo.pageCount = b;
        $.post("/ssm/post/query", danghui.searchInfo, function (backData) {
            danghui.PostArr = backData.data.currentData;
            danghui.totalCount = backData.data.totalCount;
        });
    }

    getData(1, 10);

</script>
</body>
</html>